package gui.pannelPartie.MatchList;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.Printable;

import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.Sides;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class PrintButton extends JButton implements ActionListener {


	private static final long serialVersionUID = 1L;

	private Printable toPrint;
	
	public PrintButton(Printable p) {
		super("Imprimer");
		setMargin(new Insets(0,0,0,10));
		setIcon(new ImageIcon(html.Logo.PRINT));
		addActionListener(this);
		toPrint=p;
	}

	public void actionPerformed(ActionEvent arg0) {
		PrintService imprimante = PrintServiceLookup.lookupDefaultPrintService();
		DocFlavor psInFormat=DocFlavor.SERVICE_FORMATTED.PRINTABLE;
		SimpleDoc myDoc = new SimpleDoc(toPrint, psInFormat, null);
		PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
		aset.add(new Copies(1));
		aset.add(MediaSizeName.ISO_A4);
		aset.add(Sides.ONE_SIDED);
		DocPrintJob job =imprimante.createPrintJob();
		try {
			job.print(myDoc, aset);
		} catch (PrintException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
